<?php
/**
 * User:chendenan
 * Created On 2022/3/19 0019 18:57
 */
namespace app\admin\controller;
use think\facade\Filesystem;

class Image extends  AdminBase{
    //默认图片上传
    public function upload(){
        if(!$this->request->isPost()){
            return show("请求不合法",config("status.error"));
        }
        $file = $this->request->file("file");
        /*// 设定文件上传的大小
        $fileSize = 1024 * 1024 * 2; //2M
        $fileExt = 'jpg,jpeg,png,bmp,gif';
        $fileMime = 'image/jpeg,image/png,image/gif';//这个一定要加上
        $image = "600,600";
        $rule = [
            'fileSize' => $fileSize,
            'fileExt' => $fileExt,
            'image' => $image,
            'fileMime' => $fileMime,
        ];
        try {
            validate(['file'=>$rule])->check(['file' => $file]);
        } catch (\think\exception\ValidateException $e) {
            return show($file->getOriginalName().$e->getMessage(),config("status.error"));
        }*/
        $filename = Filesystem::disk("public")->putFile("upload/image",$file);
        if(!$filename){
            return show("上传图片失败",config("status.error"));
        }
        $filename = str_replace('\\','/',config("filesystem.disks.public.url").'/'.$filename);
        $imageUrl = [
            'image'=>$filename,
        ];
        return show("上传图片成功",config("status.success"),$imageUrl);
    }
    public function layUpload(){
        if(!$this->request->isPost()){
            return show("请求不合法",config("status.error"));
        }
        $file = $this->request->file("file");
        /*// 设定文件上传的大小
        $fileSize = 1024 * 1024 * 2;
        $fileExt = 'jpg,jpeg,png,bmp,gif';
        $fileMime = 'image/jpeg,image/png,image/gif';//这个一定要加上
        $image = "600,600";
        try {
            validate(['file'=>'fileSize:'. $fileSize .'|fileExt:'.$fileExt.'|image:'.$image.'|fileMime:'.$fileMime])->check(['file' => $file]);
        } catch (\think\exception\ValidateException $e) {
            return show($file->getOriginalName().$e->getMessage(),config("status.error"));
        }*/
        $filename = Filesystem::disk("public")->putFile("upload/image",$file);
        if(!$filename){
            return json(["code" => 1, "data" => []],200);
        }
        $filename = str_replace('\\','/',config("filesystem.disks.public.url").'/'.$filename);
        $result = [
            "code" => 0,
            "data" => [
                "src" =>$filename,
            ],
        ];
        return json($result,200);
    }
}